package com.bsy.common.utils

import android.content.Context
import android.content.res.Resources
import android.view.View
import android.view.ViewGroup
import androidx.annotation.ArrayRes
import androidx.annotation.ColorRes
import androidx.annotation.DimenRes
import androidx.annotation.DrawableRes
import androidx.annotation.IntegerRes
import androidx.annotation.LayoutRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView
import com.bsy.framework.helper.AppHelper
import com.bsy.framework.manager.AppManager
import java.math.BigDecimal
import java.text.DecimalFormat

/**
 * Description: 资源操作相关
 */

fun Context.color(@ColorRes id: Int) = ContextCompat.getColor(this, id)

fun Context.string(@StringRes id: Int) = resources.getString(id)

fun Context.stringArray(@ArrayRes id: Int): Array<String> = resources.getStringArray(id)

fun Context.drawable(@DrawableRes id: Int) = ContextCompat.getDrawable(this, id)

fun Context.dimenPx(@DimenRes id: Int) = resources.getDimensionPixelSize(id)

fun Context.integer(@IntegerRes id: Int) = resources.getInteger(id)


fun View.color(@ColorRes id: Int) = context.color(id)

fun View.string(@StringRes id: Int) = context.string(id)

fun View.stringArray(@ArrayRes id: Int) = context.stringArray(id)

fun View.drawable(@DrawableRes id: Int) = context.drawable(id)

fun View.dimenPx(@DimenRes id: Int) = context.dimenPx(id)

fun View.integer(@IntegerRes id: Int) = context.integer(id)


fun Fragment.color(@ColorRes id: Int) = requireContext().color(id)

fun Fragment.string(@StringRes id: Int) = requireContext().string(id)

fun Fragment.stringArray(@ArrayRes id: Int) = requireContext().stringArray(id)

fun Fragment.drawable(@DrawableRes id: Int) = requireContext().drawable(id)

fun Fragment.dimenPx(@DimenRes id: Int) = requireContext().dimenPx(id)

fun Fragment.integer(@IntegerRes id: Int) = requireContext().integer(id)


fun RecyclerView.ViewHolder.color(@ColorRes id: Int) = itemView.color(id)

fun RecyclerView.ViewHolder.string(@StringRes id: Int) = itemView.string(id)

fun RecyclerView.ViewHolder.stringArray(@ArrayRes id: Int) = itemView.stringArray(id)

fun RecyclerView.ViewHolder.drawable(@DrawableRes id: Int) = itemView.drawable(id)

fun RecyclerView.ViewHolder.dimenPx(@DimenRes id: Int) = itemView.dimenPx(id)

fun RecyclerView.integer(@IntegerRes id: Int) = context.integer(id)

inline val Context.layoutInflater: android.view.LayoutInflater
    get() = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as android.view.LayoutInflater

fun inflateLayout(
    @LayoutRes layoutId: Int,
    parent: ViewGroup? = null,
    attachToParent: Boolean = false
): View {
    return context.layoutInflater.inflate(layoutId, parent, attachToParent)
}


fun dp2px(dpValue: Float): Int {
    return (dpValue * res.displayMetrics.density + 0.5f).toInt()
}

fun px2dp(pxValue: Float): Int {
    return (pxValue / res.displayMetrics.density + 0.5f).toInt()
}

fun sp2px(spValue: Float): Int {
    return (spValue * res.displayMetrics.scaledDensity + 0.5f).toInt()
}

fun px2sp(pxValue: Float): Int {
    return (pxValue / res.displayMetrics.scaledDensity + 0.5f).toInt()
}

fun Number.dp(): Int = dp2px(toFloat())

fun Number.sp(): Int = sp2px(toFloat())

val screenWidth: Int
    get() {
        return AppManager.getScreenWidthPx()
    }

val screenHeight: Int
    get() {
        return AppManager.getScreenHeightPx()
    }

private val res: Resources
    get() = context.resources

private val context: Context
    get() = AppHelper.getApplication()

inline val Context.asActivity
    get() = this as FragmentActivity

//数字转百分比 ，0.50->50%
inline val String.formatWithPercent: String
    get() = run {
        return runCatching {
            BigDecimal(this).multiply(BigDecimal("100")).stripTrailingZeros()
                .toPlainString() + "%"
        }.getOrDefault(this)
    }

//数字格式化，100000->100,000
inline val String.formatWithComma: String
    get() = run {
        return runCatching {
            val df = DecimalFormat()
            df.applyPattern(",###.########")
            df.format(toDouble())
        }.getOrDefault(this)
    }
